<?php
namespace app\wechat\controller;

class RStore extends Common
{
    // 维修店中心页面
    public function index()
    {

        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $param['member_id'] = session('member_id');
        $param['terrace'] = 'weixin';
        $repair = call_interface('repair_store_index',$param);
        if($repair["code"]!=200) {
            //$repair["data"] = [];
            //print_r($repair);exit();
            $this->redirect('base/error');
        }
        $this->assign('repair', $repair["data"]);

        return view();
    }

    // 维修店修改信息
    public function store_message()
    {
        /*PJ非微信本地测试方法*/
        if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

        $param['member_id'] = session('member_id');
        $param['terrace'] = 'weixin';
        $repair = call_interface('repair_store_index',$param);
        if($repair["code"]!=200) {
            $repair["data"] = [];
        }
        $this->assign('repair', $repair["data"]);


        return view();
    }

    // 修改维修店信息
    public function repair_info_change()
    {

        if (request()->isAjax()) {
            /*PJ非微信本地测试方法*/
            if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

            $param['member_id'] = session('member_id');
            $param['fields'] = input('param.fields');
            $param['values'] = input('param.values');
            $repair = call_interface('repair_info_change',$param);
            return $repair;

        }
        $this->redirect('base/error');

    }

    // 修改维修店店鋪经营范围
    public function repair_item_change()
    {

        if (request()->isAjax()) {
            /*PJ非微信本地测试方法*/
            if( @intval($_COOKIE["PJ"]) ){session('member_id',@intval($_COOKIE["PJ"]));}

            $param['member_id'] = session('member_id');
            $param['adds'] = input('param.add_id');
            $param['dels'] = input('param.del_id');
            $repair = call_interface('repair_item_change',$param);
            return $repair;


        }
        $this->redirect('base/error');

    }


    // 维修店店鋪保障金
    public function repair_ketubbah_pay()
    {

        if (request()->isAjax()) {

            $param['member_id'] = session('member_id');
            $payMoney = config('system.repair_shop_margin');
            $repair = db('repair')->where('member_id',$param['member_id'])->find();
            if($repair["money"]>=$payMoney){
                $data = [
                    "money"=>['exp','money-'.$payMoney],
                    "ketubbah"=>$payMoney,
                    "ketubbah_time"=>time()
                ];
                $update = db('repair')->where('member_id',$param['member_id'])->update($data);
                if($update){
                    $data = [
                        "member_id"=>$param['member_id'],
                        "money"=>-$payMoney,
                        "frozen_status"=>0,
                        "type"=>1,
                        "msg"=>"维修店缴纳保障金",
                        "source"=>9,
                        "add_time"=>time(),
                        "status"=>1,
                        "pay_type"=>'余额'
                    ];
                    $insert = db('member_money')->insert($data);
                }
                $return = ['code' => 200, 'data' => '' ,'msg'=> 'SUCCESS'];
            }else{
                $return = ['code' => 400040, 'data' => '' ,'msg'=> '余额不足'];
            }
            return json($return);

        }
        $this->redirect('base/error');

    }



    // 维修店店鋪退保障金
    public function repair_ketubbah_refund()
    {

        if (request()->isAjax()) {

            $param['member_id'] = session('member_id');
            $payMoney = config('system.repair_shop_margin');
            $repair = db('repair')->where('member_id',$param['member_id'])->find();
            if($repair["ketubbah"]>0){
                $data = [
                    "money"=>['exp','money+'.$repair["ketubbah"]],
                    "ketubbah"=>0,
                    "ketubbah_time"=>0
                ];
                $update = db('repair')->where('member_id',$param['member_id'])->update($data);
                if($update){
                    $data = [
                        "member_id"=>$param['member_id'],
                        "money"=>$repair["ketubbah"],
                        "frozen_status"=>0,
                        "type"=>1,
                        "msg"=>"维修店退回保障金",
                        "source"=>8,
                        "add_time"=>time(),
                        "status"=>1,
                        "pay_type"=>'保障金'
                    ];
                    $insert = db('member_money')->insert($data);
                }
                $return = ['code' => 200, 'data' => '' ,'msg'=> 'SUCCESS'];
            }else{
                $return = ['code' => 400040, 'data' => '' ,'msg'=> '保障金不足'];
            }
            return json($return);

        }
        $this->redirect('base/error');

    }


}
